/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainFrame.java
 *
 * Created on 23-02-2011, 23:24:22
 */
package GUI;

import interact.connect;
import java.awt.Color;
import java.awt.Component;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeSelectionModel;
import org.netbeans.lib.awtextra.AbsoluteConstraints;

/**
 *
 * @author UnderGround
 */
public class MainFrame extends javax.swing.JFrame {
     
    private Image image;
    Icon leafIcon = new ImageIcon("src//Images//icon.png");
    Icon openIcon = new ImageIcon("src//Images//home2.png");
    Icon closedIcon = new ImageIcon("src//Images//home.png");
    
    /** Creates new form MainFrame */
    public MainFrame() {
        Image icon = getToolkit().getImage(getClass().getResource("/images/soft_icon.png"));
        setIconImage(icon);
        connect.configServer();
//        new interact.progress(null,true);
        initComponents();
        Tree.setCellRenderer(new treetest());
        setVisible(true);
//        setAlwaysOnTop(true);
        javax.swing.Timer t = new javax.swing.Timer(1000, new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                time.setText(new Date().toString());
            }
        });
        t.start();
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

   
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jDesktopPane1 = new javax.swing.JDesktopPane();
        image = new ImageIcon("src//Images//Main.png").getImage();
        MainPanel = new interact.ImagePanel(image);
        time = new javax.swing.JTextField();
        ContentPanel = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        DefaultMutableTreeNode library = new DefaultMutableTreeNode("HOME");
        createNodes(library);
        Tree = new javax.swing.JTree(library);
        Tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
        // Update only one tree instance
        DefaultTreeCellRenderer cellRenderer = (DefaultTreeCellRenderer)Tree.getCellRenderer();
        cellRenderer.setLeafIcon(leafIcon);
        cellRenderer.setClosedIcon(closedIcon);
        cellRenderer.setOpenIcon(openIcon);

        cellRenderer.setBackgroundNonSelectionColor(new Color(255, 255, 221));
        cellRenderer.setBackgroundSelectionColor(new Color(0, 0, 128));
        cellRenderer.setBorderSelectionColor(Color.black);
        cellRenderer.setTextSelectionColor(Color.white);
        cellRenderer.setTextNonSelectionColor(Color.blue);
        //Remove the icons
        //renderer.setLeafIcon(null);
        //renderer.setClosedIcon(null);
        //renderer.setOpenIcon(null);

        // Change defaults so that all new tree components will have new icons
        UIManager.put("Tree.leafIcon", leafIcon);
        UIManager.put("Tree.openIcon", openIcon);
        UIManager.put("Tree.closedIcon", closedIcon);
        Tree = new javax.swing.JTree(library);
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(800, 550));
        setResizable(false);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setText("jLabel1");
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 0, 0));

        MainPanel.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 204, 204), 2, true));
        MainPanel.setMaximumSize(new java.awt.Dimension(800, 550));
        MainPanel.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        time.setEnabled(false);
        MainPanel.add(time, new org.netbeans.lib.awtextra.AbsoluteConstraints(581, 44, 178, -1));

        ContentPanel.setBackground(new java.awt.Color(153, 153, 255));
        ContentPanel.setPreferredSize(new java.awt.Dimension(612, 407));
        ContentPanel.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
        MainPanel.add(ContentPanel, new org.netbeans.lib.awtextra.AbsoluteConstraints(178, 117, 620, 410));

        jPanel2.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        Tree.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        Tree.setRowHeight(22);
        Tree.setVisibleRowCount(30);
        Tree.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
            public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
                TreeValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(Tree);

        jPanel2.add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 180, 410));

        MainPanel.add(jPanel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(2, 117, -1, 410));

        MainPanel.setBounds(0, 0, 800, 550);
        jDesktopPane1.add(MainPanel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        getContentPane().add(jDesktopPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 800, 529));

        jMenuBar1.setPreferredSize(new java.awt.Dimension(56, 20));

        jMenu1.setText("File");
        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");
        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-816)/2, (screenSize.height-588)/2, 816, 588);
    }// </editor-fold>//GEN-END:initComponents

    private void TreeValueChanged(javax.swing.event.TreeSelectionEvent evt) {//GEN-FIRST:event_TreeValueChanged
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) Tree.getLastSelectedPathComponent();
        Object nodeInfo = node.getUserObject();
        if (node.isLeaf()) {
            System.out.println(nodeInfo.toString());
            if (nodeInfo == "My account") {
                frmAdministrator admin = new frmAdministrator();
                callPanel(admin);
            }
            if (nodeInfo == "View Employee") {
                frmEmployeeManager emp = new frmEmployeeManager();
                callPanel(emp);
            }

            if (nodeInfo == "Add new book") {
                AddBooks a = new AddBooks(this, true);
                a.setVisible(true);
            }
            if (nodeInfo == "Book Manager") {
                BookManager book = new BookManager();
                callPanel(book);
            }
            if (nodeInfo == "Add Department") {
            }
            if (nodeInfo == "Category manager") {
                frmCategoryManager category = new frmCategoryManager();
                callPanel(category);
            }
            if (nodeInfo == "Publisher manager") {
                frmPublisherManager publisher = new frmPublisherManager();
                callPanel(publisher);
            }
            if (nodeInfo == "Search book") {
                frmSearch search = new frmSearch();
                callPanel(search);
            }
            if (nodeInfo == "Department manager") {
            frmDepartmentManager department= new frmDepartmentManager();
                callPanel(department);
            }

            if (nodeInfo == "Change password") {
            frmChangePassword change = new frmChangePassword();
                callPanel(change);
            }
           
        }
}//GEN-LAST:event_TreeValueChanged

    public static void callPanel(JPanel jpanel) {
        ContentPanel.removeAll();
        jpanel.setVisible(false);
        ContentPanel.add(jpanel, new AbsoluteConstraints(0, 0, ContentPanel.getWidth(), ContentPanel.getHeight()));
        jpanel.setVisible(true);
    }

    private void createNodes(DefaultMutableTreeNode top) {
        DefaultMutableTreeNode category = null;
        DefaultMutableTreeNode emp = null;
        DefaultMutableTreeNode acc = null;
        DefaultMutableTreeNode book = null;
        DefaultMutableTreeNode IssueBook = null;

        category = new DefaultMutableTreeNode("Employee manager");
        top.add(category);

        emp = new DefaultMutableTreeNode("View Employee");
        category.add(emp);

        emp = new DefaultMutableTreeNode("Add employee");
        category.add(emp);

        emp = new DefaultMutableTreeNode("Department manager");
        category.add(emp);

        category = new DefaultMutableTreeNode("Account manager");
        top.add(category);

        acc = new DefaultMutableTreeNode("My account");
        category.add(acc);

        acc = new DefaultMutableTreeNode("Change password");
        category.add(acc);

        category = new DefaultMutableTreeNode("Book management");
        top.add(category);

        book = new DefaultMutableTreeNode("Search book");
        category.add(book);

        book = new DefaultMutableTreeNode("Add new book");
        category.add(book);
        
        book = new DefaultMutableTreeNode("Book Manager");
        category.add(book);

        book = new DefaultMutableTreeNode("Publisher manager");
        category.add(book);

        book = new DefaultMutableTreeNode("Category manager");
        category.add(book);

    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new MainFrame().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    public static javax.swing.JPanel ContentPanel;
    private javax.swing.JPanel MainPanel;
    private javax.swing.JTree Tree;
    private javax.swing.JDesktopPane jDesktopPane1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField time;
    // End of variables declaration//GEN-END:variables
}
 class treetest extends DefaultTreeCellRenderer{
 @Override
    public Component getTreeCellRendererComponent(JTree tree,
      Object value,boolean sel,boolean expanded,boolean leaf,int row,boolean hasFocus) {

        super.getTreeCellRendererComponent(tree, value, sel,
          expanded, leaf, row, hasFocus);

        //Object nodeObj = ((DefaultMutableTreeNode)value).getUserObject();
        if(value.toString().equalsIgnoreCase("View Employee")){
            System.out.println("ok");
            ImageIcon view = new ImageIcon("src//Images//Search.png");
            setIcon(view);
        }
        return this;

    }
}
